<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserAgent;
use plugin\ycMassage\app\model\MassageUserAgentApply;
use plugin\ycMassage\app\model\MassageUserAgentStatistics;
use plugin\ycMassage\app\validate\MassageUserAgentApply as ValidateMassageUserAgentApply;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\NoticeType;
use plugin\ycMassage\utils\notice\Notice;
use support\Request;
use think\facade\Db;

class AgentController extends BaseController
{
    protected $noNeedLogin = ["apply"];
    public function info(Request $request)
    {
        $model = MassageUserAgentApply::where(['uid' => $request->uid, 'saas_appid' => $request->saas_appid])
            ->whereIn('examine', [
                Examine::WAIT['value'],
                Examine::NO['value'],
            ])->order('id desc')->find();
        if ($model) {
            return $this->successRes($model);
        }
        return $this->fail('未找到代理申请信息');
    }
    public function user(Request $request)
    {
        $model = MassageUserAgent::where(['uid' => $request->uid, 'saas_appid' => $request->saas_appid])->order('state desc,id asc')->select();
        if (!$model->isEmpty()) {
            $model->each(function ($item) {
                $item->title = "{$item->name} - {$item->agent_level}";
            });
            return $this->successRes($model);
        }
        return $this->fail('未找到代理申请信息');
    }
    public function apply(Request $request)
    {
        $D = $request->post();
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        if (!$uid) {
            $uid = null;
            $saas_appid = $request->appid;
        }
        $validate = new ValidateMassageUserAgentApply;
        if (!$validate->check($D)) {
            return $this->fail($validate->getError());
        }
        Db::startTrans();
        try {
            $model = new MassageUserAgentApply;
            $model->uid = $uid;
            $model->saas_appid = $saas_appid;
            $model->mobile = $D['mobile'];
            $model->name = $D['name'];
            $name = $model->name;
            if (!empty($D['province_id'])) {
                $model->province_id = (int)$D['province_id'];
                $model->province = $D['province'];
                $name = "({$model->province})" . $name;
            }
            if (!empty($D['city_id'])) {
                $model->city_id = (int)$D['city_id'];
                $model->city = $D['city'];
                $name = "({$model->city})" . $name;
            }
            if (!empty($D['area_id'])) {
                $model->area_id = (int)$D['area_id'];
                $model->area = $D['area'];
                $name = "({$model->area})" . $name;
            }
            $model->examine = Examine::WAIT['value'];
            $model->save();
            $notice = new Notice();
            $notice->saas_appid = $model->saas_appid;
            $notice->role = ['ADMIN'];
            $notice->type = NoticeType::WORK_ORDER['value'];
            $notice->setData([
                'service_name' => '代理申请审核',
                'name' => $name,
                'time' => date('Y-m-d H:i:s'),
            ]);
            $notice->send();
            Db::commit();
            return $this->success('申请成功,请等待审核');
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
    }
    public function statistics(Request $request)
    {
        $agent_id = $request->get('agent_id');
        $date = $request->get('date');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $model = MassageUserAgentStatistics::where(['uid' => $uid, 'agent_id' => $agent_id, 'saas_appid' => $saas_appid]);
        switch ($date) {
            case 'yesterday':
                $model->whereDay('create_time', 'yesterday');
                break;
            case 'week':
                $model->whereWeek('create_time');
                break;
            case 'month':
                $model->whereMonth('create_time');
                break;
            case 'last_month':
                $model->whereMonth('create_time', 'last month');
                break;
            default:
                $model->whereDay('create_time');
                break;
        }
        $MassageUserAgentStatistics = $model->select();
        $data = [
            'order_num' => 0,
            'order_price' => 0,
            'duration' => 0,
            'new_customers' => 0,
            'commission' => 0,
            'browse_num' => 0,
        ];
        foreach ($MassageUserAgentStatistics as $item) {
            $data['order_num'] += $item->order_num;
            $data['order_price'] += $item->order_price;
            $data['duration'] += $item->duration;
            $data['new_customers'] += $item->new_customers;
            $data['commission'] += $item->commission;
            // $data['browse_num'] += $item->browse_num;
        }
        return $this->successRes($data);
    }
}
